// input.cpp : contains the definition of the input component, which allows objects
// to obtain user input from the keyboard/mouse
//$Id: Input.cpp 19 2009-08-16 00:26:19Z brwarner2 $
#include "stdafx.h"
#include "Input.h"
#include "GameMonkey.h"
#include "ObjectManager.h"
#include "ResourceManager.h"

void CInput::RegisterGM(gmMachine *pGM)
{
	//Register the keys
#define MAKEKEY(let) pGM->GetGlobals()->Set(pGM, #let, gmVariable((int)let));
	MAKEKEY(KEY_A)MAKEKEY(KEY_B)MAKEKEY(KEY_C)MAKEKEY(KEY_D)MAKEKEY(KEY_D)MAKEKEY(KEY_E)MAKEKEY(KEY_F)MAKEKEY(KEY_G)MAKEKEY(KEY_H)MAKEKEY(KEY_I)MAKEKEY(KEY_J)MAKEKEY(KEY_K)MAKEKEY(KEY_L)MAKEKEY(KEY_M)MAKEKEY(KEY_N)MAKEKEY(KEY_O)MAKEKEY(KEY_P)MAKEKEY(KEY_Q)MAKEKEY(KEY_R)MAKEKEY(KEY_S)MAKEKEY(KEY_T)MAKEKEY(KEY_U)MAKEKEY(KEY_V)MAKEKEY(KEY_W)MAKEKEY(KEY_X)MAKEKEY(KEY_Y)MAKEKEY(KEY_Z);
	MAKEKEY(KEY_UP)MAKEKEY(KEY_DOWN)MAKEKEY(KEY_LEFT)MAKEKEY(KEY_RIGHT);
#undef MAKEKEY

	//Register functions
	CObjectManager* pObjects = GetGlobalObjectManager(*pGM);
	pObjects->RegisterMemberFunction(COMPONENT_ID, "IsKeyDown");
}

int CInput::GMCall(gmThread *a_thread, const std::string &name)
{
	if( name == "IsKeyDown" )
	{
		//Get key
		GM_CHECK_NUM_PARAMS(1);
		GM_CHECK_INT_PARAM( key, 0 );

		//Check if it is down
		bool down = Resource()->GetInput()->IsPressed(bug::input::DEVICE_KEYBOARD, key);
		a_thread->PushInt(down);
		return GM_OK;
	}
	return GM_EXCEPTION;
}